package com.zhoukx.nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 *
 *  零拷贝  IO的时间
 *
 */
public class NewIOServer {

    public static void main(String[] args) throws IOException {

        //  创建连接通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        //监听端口
        serverSocketChannel.socket().bind(new InetSocketAddress(7001));

        //创建buffer
        ByteBuffer allocate = ByteBuffer.allocate(4096);

        while (true) {

            // 连接通道
            SocketChannel accept = serverSocketChannel.accept();

            int readCount = 0;

            while (-1 != readCount) {

                try {

                    // 读取通道的流,到缓存中去
                    readCount = accept.read(allocate);

                }catch (Exception e){

                    break;

                }
                // 倒带  让position  =0
                allocate.rewind();
            }


        }


    }
}
